package com.zmn.oms.dubbo.impl.redistridute;

import com.google.common.base.Throwables;
import com.jd.open.api.sdk.internal.JSON.JSON;
import com.zmn.common.dto2.AMISListData;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.common.dio.redistribute.*;
import com.zmn.oms.common.dio.redistribute.audit.ManagerAuditQuery;
import com.zmn.oms.common.dro.redistribute.*;
import com.zmn.oms.common.dro.redistribute.audit.ApplyDetailDRO;
import com.zmn.oms.common.dro.redistribute.audit.ApplyItemDRO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.interfaces.redistridute.OrderWorkRedistributeListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.bo.redistribute.CheckApplyRedistributeBO;
import com.zmn.oms.model.bo.redistribute.CheckOperateBO;
import com.zmn.oms.model.entity.redistribute.OrderWorkRedistributeApply;
import com.zmn.oms.services.interfaces.redistribute.OrderWorkRedistributeApplyService;
import com.zmn.oms.zmn.business.interfaces.redistribute.OrderWorkRedistributeInfoBService;
import com.zmn.oms.zmn.business.interfaces.redistribute.OrderWorkRedistributeListBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * @author sunlife
 * @date:2022/5/9 下午4:30
 * description:
 */
@Slf4j
@DubboService(loadbalance = "roundrobin", timeout = 10000, retries = 2, version = DubboConsts.INTERFACE_VERSION)
public class OrderWorkRedistributeListRemoteServiceImpl implements OrderWorkRedistributeListRemoteService {

    @Autowired
    OrderWorkRedistributeListBService orderWorkRedistributeListBService;
    @Autowired
    OrderWorkRedistributeApplyService orderWorkRedistributeApplyService;
    @Resource
    private OrderWorkRedistributeInfoBService redistributeInfoBService;

    @Override
    public ResponseDTO<CheckOperateDRO> checkCanOperateRedistribute(@Valid @NotNull(message = "orderId不能为空") Long orderId, @Valid @NotNull(message = "workId不能为空") Long workId) {
        try {
            CheckOperateBO bo = orderWorkRedistributeListBService.checkCanOperateRedistribute(orderId, workId);
            CheckOperateDRO dro = BeanMapper.map(bo, CheckOperateDRO.class);
            return ResponseDTO.success(dro);
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[%s]校验是否可以操作改派失败", orderId), e);
            return ResponseDTO.fail("校验失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<CheckApplyRedistributeDRO> checkCanApplyRedistribute(CheckApplyRedistributeDIO checkApplyRedistributeDIO) {
        try {
            CheckApplyRedistributeBO checkApplyRedistributeBO = orderWorkRedistributeListBService.checkCanApplyRedistribute(checkApplyRedistributeDIO);
            CheckApplyRedistributeDRO dro = BeanMapper.map(checkApplyRedistributeBO, CheckApplyRedistributeDRO.class);
            return ResponseDTO.success(dro);
        } catch (Exception e) {
            log.error(String.format("[%s]校验是否可以申请改派失败。", checkApplyRedistributeDIO.getOrderId()), e);
            return ResponseDTO.fail("校验失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<Boolean> checkServingDuplicateOrder(OrderWorkRedistributeDuplicateDIO dio) {
        try {
            RedistributeServingDuplicateOrderDIO redistributeServingDuplicateOrderDIO = new RedistributeServingDuplicateOrderDIO();
            BeanMapper.copy(dio, redistributeServingDuplicateOrderDIO);
            Boolean hasOnServingDuplicate = redistributeInfoBService.checkServingDuplicateOrder(redistributeServingDuplicateOrderDIO);
            return ResponseDTO.success(hasOnServingDuplicate);
        } catch (Exception e) {
            log.error("校验在途重复异常 orderId={} error={}", dio.getOrderId(), Throwables.getStackTraceAsString(e));
        }
        return ResponseDTO.success(Boolean.FALSE);
    }

    @Override
    public ResponseDTO<List<EngineerWorkRedistributeDRO>> listGrabWorkByQuery(@Valid EngineerWorkRedistributeQuery engineerWorkRedistributeQuery) {
        try {
            RedistributeWorkQuery query = BeanMapper.map(engineerWorkRedistributeQuery, RedistributeWorkQuery.class);
            List<EngineerWorkRedistributeDRO> orderList = orderWorkRedistributeListBService.listGrabWorkByQuery(query);
            return ResponseDTO.success(orderList);
        } catch (Exception e) {
            log.error(String.format("[%s]获取抢单列表失败", com.alibaba.fastjson.JSON.toJSONString(engineerWorkRedistributeQuery)), e);
            return ResponseDTO.fail("获取抢单列表失败");
        }
    }

    @Override
    public ResponseDTO<AMISListData<ApplyItemDRO>> listApplyByQuery(@Valid ManagerAuditQuery query) {
        try {
            AMISListData<ApplyItemDRO> amisData = orderWorkRedistributeListBService.listApplyByQuery(query);
            return ResponseDTO.success(amisData);
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[%s]获取列表失败", JSON.toString(query)), e);
            return ResponseDTO.fail("获取列表失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<ApplyDetailDRO> getApplyById(@Valid @NotNull(message = "改派申请ID不能为空") Long redistributeId) {
        try {
            ApplyDetailDRO applyDetail = orderWorkRedistributeListBService.getApplyById(redistributeId);
            return ResponseDTO.success(applyDetail);
        } catch (OmsBaseException e) {
            return ResponseDTO.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error(String.format("[%s]获取改派详情", JSON.toString(redistributeId)), e);
            return ResponseDTO.fail("获取改派详情，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<List<ApplyDRO>> getApplyByOrderIdAndWorkId(@Valid @NotNull(message = "orderId不能为空") Long orderId, @Valid @NotNull(message = "workId不能为空") Long workId) {

        try {
            OrderWorkRedistributeApplyQuery query = new OrderWorkRedistributeApplyQuery();
            query.setOrderId(orderId);
            query.setWorkId(workId);
            List<OrderWorkRedistributeApply> applyList = orderWorkRedistributeApplyService.listByQuery(query);
            return ResponseDTO.success(BeanMapper.mapList(applyList, ApplyDRO.class));
        } catch (Exception e) {
            log.error(String.format("[%s]获取订单改派列表失败", orderId), e);
            return ResponseDTO.fail("获取订单改派列表失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<List<EngineerDRO>> listAutoRedistributeEngineer(@Valid @NotNull(message = "orderId不能为空") Long orderId
            , @Valid @NotNull(message = "workId不能为空") Long workId
            , Date dutyTime) {
        try {
            List<EngineerDRO> engineerList = orderWorkRedistributeListBService.listRedistributeEngineer(orderId, workId, dutyTime);
            return ResponseDTO.success(engineerList);
        } catch (Exception e) {
            log.error(String.format("[%s]获取工程师列表失败", JSON.toString(orderId)), e);
            return ResponseDTO.fail("获取工程师列表失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<List<EngineerDRO>> listRedistributeEngineer(@Valid @NotNull(message = "查询参数不能为空") EngineerRedistributeQuery query) {
        try {
            log.info("#OrderWorkRedistributeListRemoteServiceImpl#listRedistributeEngineer:{}", query);
            List<EngineerDRO> engineerList = orderWorkRedistributeListBService.listRedistributeEngineer(query);
            return ResponseDTO.success(engineerList);
        } catch (Exception e) {
            log.error(String.format("[%s]获取工程师列表失败", JSON.toString(query.getOrderId())), e);
            return ResponseDTO.fail("获取工程师列表失败，请稍后重试");
        }
    }

    @Override
    public ResponseDTO<OrderWorkRedistributeStatusDRO> getRedistributeStatusById(@Valid @NotNull(message = "改派申请ID不能为空") Long redistributeId) {
        try {
            OrderWorkRedistributeStatusDRO orderWorkRedistributeStatusDRO = orderWorkRedistributeListBService.getRedistributeStatusById(redistributeId);
            return ResponseDTO.success(orderWorkRedistributeStatusDRO);
        } catch (Exception e) {
            log.error(String.format("[%s]获取状态失败", JSON.toString(redistributeId)), e);
            return ResponseDTO.fail("获取状态失败，请稍后重试");
        }
    }
}
